package com.tensquare.notice.controller;

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.notice.client.ArticleClient;
import com.tensquare.notice.client.UserClient;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import com.tensquare.notice.service.NoticeFreshService;
import com.tensquare.notice.service.NoticeService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.aspectj.weaver.ast.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author wz
 * @company 公司
 * @create 2020-02-15 12:32
 */
@RestController
@RequestMapping("notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeFreshService noticeFreshService;
    //根据ID查询消息通知
    @GetMapping("{id}")
    public Result selectById(@PathVariable String id) {
        Notice notice = noticeService.selectById(id);
        return new Result(true, StatusCode.OK, "查询成功", notice);
    }
    //根据分页条件查询消息同桌http://127.0.0.1:9014/notice/search/{page}/{size}  POST
    @PostMapping("search/{page}/{size}")
    public Result selectByList(@RequestBody Notice notice,
                               @PathVariable Integer page,
                               @PathVariable Integer size){
        Page<Notice> pageData = noticeService.selectByPage(notice,page,size);

        PageResult<Notice> pageResult = new PageResult<>(pageData.getTotal(),pageData.getRecords());

        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    //新增通知
    @PostMapping
    public Result save(@RequestBody Notice notice){
        noticeService.save(notice);
        return new Result(true, StatusCode.OK,"新增成功");
    }
    //修改通知
    @PutMapping
    public Result update(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return new Result(true,StatusCode.OK,"修改成功");
    }
    // 5. 根据用户id查询该用户的待推送消息（新消息）    
    // http://127.0.0.1:9014/notice/fresh/{userId}/{page}/{size}  GET
    @GetMapping("fresh/{userId}/{page}/{size}")
    public Result freshPage(@PathVariable String userId,
                            @PathVariable Integer page,
                            @PathVariable Integer size){
      Page<NoticeFresh> pageData =  noticeService.freshPage(userId,page,size);

      PageResult<NoticeFresh>  pageResult = new PageResult<>(pageData.getTotal(),pageData.getRecords());

      return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    //删除待推送消息
    @DeleteMapping("fresh")
    public Result freshDelete(@RequestBody  NoticeFresh noticeFresh){
        noticeFreshService.freshDelete(noticeFresh);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
